<?php
class AppNoteAction extends Action { 
	/**
	 * 帖子列表
	 * 入参,排序字段,用户id
	 * get
 	**/
    public function noteList(){
    	/*if (!$this->isGet()){
            $result['code']='0';
        	$result['msg']="请求错误";
        	$this->ajaxReturn($result,'JSON');
        }*/
        $note = M('Note');
        $str = $_GET['str'];
        if(!empty($str)){
        	$where['s_note.type'] = $str;
        }
        if(empty($_GET['id'])){
        	$noteList = $note ->field('s_note.*,u.username,ifnull(note.note_count,0) as note_count,(TO_DAYS(now()) - TO_DAYS(s_note.create_time)) as days') ->join("left join s_user u on u.id=s_note.user_id 
        	left join (select count(id) as note_count,note_id from s_note_one group by note_id) as note on note.note_id= s_note.id")-> order('s_note.top desc,s_note.create_time desc')->where($where) -> select();
        }else{
	        $where['s_note.user_id'] = $_GET['id'];//我的帖子
	        $noteList = $note ->field('s_note.*,u.username,ifnull(note.note_count,0) as note_count,(TO_DAYS(now()) - TO_DAYS(s_note.create_time)) as days') ->join("left join s_user u on u.id=s_note.user_id 
        	left join (select count(id) as note_count,note_id from s_note_one group by note_id) as note on note.note_id= s_note.id")->where($where)-> order('s_note.top desc,s_note.create_time desc') -> select();
        }
		
//        echo var_dump($courseList);
//        $this->display();
		$result['noteList'] = $noteList;
	    $this->ajaxReturn($result,'JSON');
    }
    /**
     * 帖子详细
     * 入参,课程id
     * get
     */
    public function noteDitail(){
    	$note = M('Note');
    	$where['id'] = $_GET['id'];//$_GET['id']
    	$count=$note->where($where)->getField('number');
    	//查看列表查看次数加一
    	$counts = (int)$count + 1;
    	$data['number'] = $counts;
    	$note->where($where)->data($data)->save();
    	//课程
    	$note = M('Note');
    	$where_id['s_note.id'] = $_GET['id'];
    	$noteList = $note ->field('s_note.*,u.username,note.note_count,u.image as image_user,(TO_DAYS(now()) - TO_DAYS(s_note.create_time)) as days') ->join("left join s_user u on u.id=s_note.user_id 
        	left join (select count(id) as note_count,note_id from s_note_one group by note_id) as note on note.note_id= s_note.id")->where($where_id)->find();
    	//评论
    	$note_one = M('Note_one');
    	$note_id['note_id'] = $_GET['id'];
    	$noteOneList = $note_one->field('s_note_one.*,u.username,u.image as image_user,(TO_DAYS(now()) - TO_DAYS(s_note_one.create_time)) as days')->join("left JOIN s_user u on s_note_one.user_id = u.id ")->where($note_id)->select();
    	//二级评论
    	$note_two = M('Note_two');
    	$note_id_two["o.note_id"] = $_GET['id'];
    	$noteTwoList = $note_two->field('s_note_two.*,u.username as name,uu.username as twoname,(TO_DAYS(now()) - TO_DAYS(s_note_two.create_time)) as days')->where($note_id_two)->join("left JOIN s_user u on s_note_two.user_id = u.id left JOIN s_note_one o on s_note_two.note_one_id = o.id left JOIN s_user uu on o.user_id = uu.id  ")->select();
    	
    	$result['noteList'] = $noteList;//列表
    	$result['noteOneList'] = $noteOneList;//列表评论
    	$result['noteTwoList'] = $noteTwoList;//二级评论
    	$this->ajaxReturn($result,'JSON');
    }
    /**
     * 新增帖子
     * 入参
     * post
     */
    public function addNote(){
    	$note = M('Note');
    	$note->create();
    	$notes['title']=$_POST['title'];
    	$notes['user_id']=$_POST['user_id'];
    	$notes['content']=$_POST['content'];
    	$notes['type']=$_POST['type'];
    	$notes['create_time']=date('Y-m-d H:i',time());
    	$suc = $note->data($notes)->add();
    	$this->ajaxReturn($suc,'JSON');
    }
    /**
     * 回复楼主
     * 入参user_id(session),note_id,message
     * post
     */
    public function noteOneAdd(){
    	
    	$note_one = M('Note_one');
    	$note_one->create();
    	$noteOne['note_id']=$_POST['note_id'];
    	$noteOne['user_id']=$_POST['user_id'];
    	$noteOne['message']=$_POST['message'];
    	$noteOne['create_time']=date('Y-m-d H:i',time());
    	$suc = $note_one->add($noteOne);
    	$this->ajaxReturn($suc,'JSON');
    }
    /**
     * 回复评论
     * 入参user_id(session),note_id,note_one_id,words
     * post
     */
    public function noteTwoAdd(){
    	
    	$note_two = M('Note_two');
    	$note_two->create();
    	$noteTwo['note_id']=$_POST['note_id'];
    	$noteTwo['note_one_id']=$_POST['note_one_id'];
    	$noteTwo['user_id']=$_POST['user_id'];
    	$noteTwo['words']=$_POST['words'];
    	$noteTwo['create_time']=date('Y-m-d H:i',time());
    	$suc = $note_two->add($noteTwo);
    	$this->ajaxReturn($suc,'JSON');
    }
}
?>